﻿using Microsoft.EntityFrameworkCore;
using MyWeb.TaskGrpc.Models;

namespace MyWeb.TaskGrpc.Context
{
    public class JobsContext:DbContext
    {
        public JobsContext(DbContextOptions<JobsContext> options) : base(options)
        {

        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // 禁用日志记录
            optionsBuilder.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddProvider(new NoopLoggerProvider())));

            // 其他的数据库配置...
        }

        /// <summary>
        /// 任务
        /// </summary>
        public DbSet<Jobs> Jobs { get; set; }
        /// <summary>
        /// 任务日志
        /// </summary>
        public DbSet<TaskLog> TaskLogs { get; set; }
    }

    // 定义一个无操作的日志记录器提供程序
    public class NoopLoggerProvider : ILoggerProvider
    {
        public ILogger CreateLogger(string categoryName) => new NoopLogger();

        public void Dispose() { }

        private class NoopLogger : ILogger
        {
            public IDisposable BeginScope<TState>(TState state) => null;

            public bool IsEnabled(LogLevel logLevel) => false;

            public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter) { }
        }
    }
}
